package org.light.framework.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * 编码与解码操作工具类
 *
 * @author de.cel
 */
public class EncoderUtil {

	private final static String UTF8 = "UTF-8";

	/**
	 * 将 URL UTF-8 编码
	 */
	public static String encodeURL(String str) {
		return encodeURL(str, UTF8);
	}

	/**
	 * URL编码
	 * 
	 * @param str
	 * @param charset 编码格式
	 * @return
	 */
	public static String encodeURL(String str, String charset) {
		String target;
		try {
			target = URLEncoder.encode(str, charset);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return target;
	}

	/**
	 * 将 URL 按UTF-8解码
	 */
	public static String decodeURL(String str) {
		return decodeURL(str, UTF8);
	}

	/**
	 * 将 URL 解码
	 * 
	 * @param str
	 * @param charset 编码格式
	 * @return
	 */
	public static String decodeURL(String str, String charset) {
		String target;
		try {
			target = URLDecoder.decode(str, charset);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return target;
	}
}
